<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <script src="/js/jquery.js"></script>
    <link rel="stylesheet" href="/plugins/bootstraptable/bootstrap-table.min.css">
</head>
<body>
<div class="container-fluid">
    <h1> 学生信息表</h1>
    <!-- Button trigger modal -->
    <form class="form-inline">
        <label for="options">查询方式</label>
        <select class="form-control" id="options" name="options">
            <option>姓名</option>
            <option>学号</option>
        </select>
        <div class="form-group">
            <input type="text" class="form-control" id="search" placeholder="请输入需要查询的关键字",name="search">
            <button type="button" class="btn btn-default btn-primary"onclick="readstudent();">查询</button>
        </div>
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" onclick="preAdd();">新增学生</button>
    </form>
    <table class="table table-bordered" id="stuTable">

    </table>

</div>
<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">学生信息</h4>
            </div>
            <form id="formstudent">
                <div class="modal-body">
                    <input hidden="hidden" id="id" name="id"/>
                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="请输入姓名" required minlength="2" maxlength="10">
                        <label id="name-error" class="error" for="name" style="color: red">姓名为必填项哦！</label>
                    </div>
                    <div class="form-group">
                        <label for="number">学号</label>
                        <input type="text" class="form-control" id="number" name="numbers" placeholder="请输入学号" required minlength="10" maxlength="10">
                        <label id="numbers-error" class="error" for="number" style="color: red">学号为必填项哦！</label>
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码" required minlength="6" maxlength="15">
                        <label id="password-error" class="error" for="password" style="color: red">密码为必填项哦！</label>
                    </div>
                    <div class="form-group">
                        <label for="sex">性别</label>
                        <select class="form-control" id="sex" name="sex">
                            <option value="0">保密</option>
                            <option value="1">男</option>
                            <option value="2">女</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="age">年龄</label>
                        <input type="text" class="form-control" id="age" name="age" placeholder="请输入年龄" required min="15" max="28">
                        <label id="age-error" class="error" for="age" style="color: red">年龄为必填项哦！</label>
                    </div>
                    <div class="form-group">
                        <label for="score">成绩</label>
                        <input type="text" class="form-control" id="score" name="score" placeholder="请输入成绩" min="0" max="100">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">提交保存</button>
                </div>
            </form>
            <!--表单结束-->
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script src="/js/bootstrap.min.js"></script>
<script src="/plugins/bootstraptable/bootstrap-table.min.js"></script>
<script src="/plugins/bootstraptable/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="/plugins/bootstraptable/jqueryvalidate/jquery.validate.min.js"></script>
<script src="/plugins/bootstraptable/jqueryvalidate/localization/messages_zh.min.js"></script>
<script type="text/javascript">

    function preAdd(){
        $("#id").val(0);//提示新增
        $("#name").val("");
        $("#number").val("");
        $("#password").val("");
        $("#sex").val("0");
        $("#age").val("");
        $("#score").val("");
    }
    function readstudent(){
        $('#stuTable').bootstrapTable("destroy");
        loadTable();
    }
    //新增或者更新
    function saveaddstudent(){
        var data=$("#formstudent").serialize();
        var id=$("#id").val();
        if(id<1)
        {

            //新增
            $.ajax({
                url: "/webapi/student/insert",
                method:"post",
                data:data
            }).done(function(){
                $('#stuTable').bootstrapTable("destroy");
                loadTable();
                $("#myModal").modal("hide");
            })
        }
        else
        {
            //更新
            $.ajax({
                url: "/webapi/student/update",
                method:"put",
                data:data
            }).done(function(){
                $('#stuTable').bootstrapTable("destroy");
                loadTable();
                $("#myModal").modal("hide");
            })
        }
    }
    function deletestudent(id){
        if (confirm("正在进行删除操作，确定吗？")){
            $.ajax({
                url: "/webapi/student/delete/"+id,
                method:"delete",
            }).done(function(){
                $('#stuTable').bootstrapTable("destroy");
                loadTable();
            })
        }//end if

    }
    function editstudent(id){
        $("#myModal").modal("show");
        $.ajax({
            url:"/webapi/student/get/"+id
        }).done(function (rs){
            $("#id").val(rs.id);
            $("#name").val(rs.name);
            $("#number").val(rs.numbers);
            $("#password").val(rs.password);
            $("#sex").val(rs.sex);
            $("#age").val(rs.age);
            $("#score").val(rs.score);
        })
    }
    function loadTable(){
        $('#stuTable').bootstrapTable({
            url: '/webapi/student/getbypage',
            striped:true,//设置true会有隔行变色效果
            pagination:true,//设置true将禁止多选
            singleSelect: false,//设置true会在底部显示分页条
            pageSize:3,//如果设置了分页，每页数据条数
            pageNumber:1,//如果设置了分页，首页页码
            sidePagination: "server",//设置在哪里进行分页，可选值“client”或者“server”
            queryParams:function (parans){
                var paraobj ={
                    size:parans.limit,
                    page:parans.offset/parans.limit,
                    sort:"id",//options.sort,
                    direct:"desc",//options.direct,
                    name:$("#search").val(),
                    farm:$("#options").val(),
                   // where:wherStr
                };
                return paraobj;
            },
            columns: [{
                checkbox: true
            }, {
                field: 'id',
                title: '编号'
            }, {
                field: 'numbers',
                title: '学号'
            }, {
                field: 'name',
                title: '姓名'
            }, {
                field: 'sex',
                title: '性别',
                formatter: function (data, row, meta) {
                    if (row.sex=="0"){
                        var operators = '保密';
                    }else if (row.sex=="1"){
                        var operators = '男';
                    }else{
                        var operators = '女';
                    }
                    return operators;
                }
            },{
                field: 'age',
                title: '年龄'
            }, {
                field: 'score',
                title: '成绩'
            }, {
                data:null,
                title: '操作1',
                width:"70px",
                formatter: function (data, row, meta) {
                    var operators = '<a type="button"  href="#" class="btn btn-mint btn-xs btn-icon" onclick=editstudent(' + row.id + ') >修改</a>';
                    return operators;
                }
            }, {
                data:null,
                title: '操作2',
                width:"70px",
                formatter: function (data, row, meta) {
                    var operators = '<a type="button"  href="#" class="btn btn-mint btn-xs btn-icon" onclick=deletestudent(' + row.id + ') >删除</a>';
                    return operators;
                }
            },]
        })
    }//end of loatTable();
    $(function (){
        $("#formstudent").validate({
            submitHandler: saveaddstudent
        });
        loadTable();
    })
</script>
</body>
</html>